<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Thông tin shop</title>
		<link rel="stylesheet" href="../css/reset.css" type="text/css" />
		<link rel="stylesheet" href="../css/common.css" type="text/css" />
		<link rel="stylesheet" href="../css/seller.css" type="text/css" />
		<script src="../scripts/jquery-1.5.2.min.js"></script>
		<script src="../scripts/common.js"></script>
		<script type="text/javascript">
		var xmlhttp;
	
		function GetXmlHttpObject()
		{
			if (window.XMLHttpRequest)
			{
				// code for IE7+, Firefox, Chrome, Opera, Safari
				return new XMLHttpRequest();
			}
			if (window.ActiveXObject)
			{
				// code for IE6, IE5
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			return null;
		}
		
		function validName()
		{
			xmlhttp = GetXmlHttpObject();
			if(xmlhttp == null)
			{
				alert("Browser do not support HTTP REQUEST");
				return;
			}
			var TenSanPham = document.getElementById("tbtensanpham").value;
			if(TenSanPham != "")
			{
				var url = "../BUS/xl_SanPham.php?TenSanPham=" + TenSanPham;
				xmlhttp.onreadystatechange=validName_Change;
				xmlhttp.open("GET",url,true);
				xmlhttp.send("null");
			}
		}
		
		function validName_Change()
		{
			if (xmlhttp.readyState==4)
			{
				document.getElementById("error").innerHTML=xmlhttp.responseText;
				ValidateName();
			}
		}
		$(document).ready(function(){
        	$("#tbtensanpham").keyup(validName).blur(validName);
			
			$("#btthemsanpham").bind("click", function()
			{
				if($("#tbtensanpham").attr("value") != "" && $("#tbGiaSanPham").attr("value") != "" && $("#tbDonViTinh").attr("value") != "" && $("#tbSoLuong").attr("value") != "")
				{
					return true;
				}
				alert("Vui lòng nhập đầy các thông tin yêu cầu");
				return false;
			});
			
			/*function validName()
			{
				var TenSanPham = $("#tbtensanpham").attr("value");
				if(TenSanPham != "")
				{
					var serverURL = "../BUS/xl_SanPham.php?TenSanPham=" + TenSanPham;
					$("#error").load(serverURL);
				}
				else
				{
					$("#error").html("");
				}
			}*/
			$("#tbGiaSanPham").keyup(validNum).blur(validNum);
			$("#tbSoLuong").keyup(validNum).blur(validNum);
			function validNum() {
				var initVal = $(this).val();
				outputVal = initVal.replace(/[^0-9\.]/g,"").replace(/^\./,"");       
				if (initVal != outputVal) {
					$(this).val(outputVal);
				}
			}
		})
        </script>
	</head>
	<body>
		<div id="page" class="them-san-pham-page">
			<?php
				include_once("../webconfig.php");
				include_once("../DAO/DataHelper.php");
				include_once("../DAO/DanhMucSanPham_DAO.php");
				include_once("../DAO/SanPham_DAO.php");
				include_once("../DAO/DataHelper.php");
				require("../UserControl/header.php");
				$listdanhmuc = DanhMucSanPham::DanhSachDanhMuc();
				
				if(isset($_SESSION["Role"]) || !isset($_SESSION["Role"]))
				{
					if($_SESSION["Role"] != 1)
					{
						$url = GetUrl("index.php");
						$message = 'Vui lòng đăng nhập trước khi sử dụng các chức năng!';
						echo "<script>javascript:alert('".$message."'); window.location = '".$url."'</script>";
					}
				}
			?>

			<div id="content">
				<?php
					$erroupload = 0;
					require("../UserControl_NguoiBan/MenuDanhMucChucNang.php");
					
					if(isset($_REQUEST['ThemSanPham']))
					{
						$temp = 0;
						if ($_FILES['uploadsanpham']['type'] == "image/gif");
						
						elseif ($_FILES['uploadsanpham']['type'] == "image/jpg");
						
						elseif ($_FILES['uploadsanpham']['type'] == "image/jpeg");
						
						elseif ($_FILES['uploadsanpham']['type'] == "image/bmp");
						
						elseif ($_FILES['uploadsanpham']['type'] == "image/png");
						
						else
						{
							$erroupload = 1;
							$temp = 1;
						}
						if($temp == 0)
						{
							if ($_FILES['uploadsanpham']['size'] > 512000)
							{
								$erroupload = 2;					
							}
							else
							{
								
								$sanpham = new SanPham;
								$sanpham->sp_ten = $_REQUEST["TenSanPham"];
								$sanpham->sp_ngay_tao = date("y/m/d",time());
								$sanpham->sp_mo_ta = $_REQUEST["MoTa"];
								$sanpham->sp_don_vi_tinh = $_REQUEST["DonViTinh"];
								$sanpham->sp_don_gia = $_REQUEST["GiaSanPham"];
								$sanpham->sp_so_luong = $_REQUEST["SoLuong"];
								$sanpham->sp_tinh_trang = 1;
								$sanpham->sp_ma_gian_hang = $_SESSION['MaGiangHang'];
								foreach($listdanhmuc as $danhmuc)
								{
									
									if($danhmuc->dm_ten == $_REQUEST["TenDanhMuc"])
									{
										$sanpham->sp_ma_danh_muc = $danhmuc->dm_ma;
										break;
									}
								}
								$type = preg_split('[/]', $_FILES['uploadsanpham']['type'],2);
								$sanpham->sp_hinh_anh = $sanpham->sp_ten . "." . $type[1];
								$_FILES["uploadsanpham"]["name"] = $sanpham->sp_hinh_anh;
								move_uploaded_file($_FILES["uploadsanpham"]["tmp_name"], "../images/".$_FILES["uploadsanpham"]["name"]);	
								$ID = SanPham::ThemSanPhamMoi($sanpham);
								$url = GetUrl("NguoiBan/ChiTietSanPham.php?MaSP=$ID");
								$message = 'Đã đăng sản phẩm thành công!';
								echo "<script>javascript:alert('".$message."'); window.location = '".$url."'</script>";
								
							}
						}
					}
				?>
				<div id="seller-shop-info" class="block layout-2col-col2">
					<div class="title">
					  <h3>Đăng Sản Phẩm Mới</h3>
				  </div>
					<div class="content">
                    <form enctype="multipart/form-data" method="post">
                        <table class="them-san-pham-table">                            
                            <tr>
								<td class="label-col"><label>Tên sản phẩm:</label></td>
								<td><input id='tbtensanpham' onkeyup="validName()" name="TenSanPham" type="text" value="<?php if(isset($_REQUEST['ThemSanPham'])) echo $_REQUEST['TenSanPham']?>" id="TenSanPham" /><label class='error' style="display:inline">*</label><span id='error'></span></td>
                            </tr>
                            <tr>
								<td class="label-col"><label>Danh Mục:</label></td>
								<td>
									<select name="TenDanhMuc" id="cbTenDanhMuc">
										<?php										
											foreach($listdanhmuc as $danhmuc)
											{
												echo "<option>$danhmuc->dm_ten</option>";
											}
										?>	
									</select>
                              </td>
                            </tr>
                            <tr>
								<td class="label-col"><label>Giá tiền:</label></td>
								<td><input name="GiaSanPham" type="text" value="<?php if(isset($_REQUEST['ThemSanPham'])) echo $_REQUEST['GiaSanPham']?>" id="tbGiaSanPham" /><label class='error' style="display:inline">*</label></td>
                            </tr>
                            <tr>
								<td class="label-col"><label>Đơn vị tính:</label></td>
								<td><input name="DonViTinh" value="<?php if(isset($_REQUEST['ThemSanPham'])) echo $_REQUEST['DonViTinh']?>" type="text" id="tbDonViTinh" /><label class='error' style="display:inline">*</label></td>
                            </tr>
                            <tr>
								<td class="label-col"><label>Số lượng:</label></td>
								<td><input name="SoLuong" value="<?php if(isset($_REQUEST['ThemSanPham'])) echo $_REQUEST['SoLuong']?>" type="text" id="tbSoLuong" /><label class='error' style="display:inline">*</label></td>
                            </tr>                            
                            <tr>
								<td class="label-col"><label>Hình Mô Tả:</label></td>
								<td><input type="file" id="image" name="uploadsanpham"/>
								<?php  
							  		if($erroupload == 1) 
										echo "</br><label style='color:#F00; font-weight:bold'>Vui lòng upload file hình thôi!</label>";
									if($erroupload == 2) 
										echo "</br><label style='color:#F00; font-weight:bold'>Chỉ được up file nhỏ hơn 50kb !</label>";
								?>     
								</td>                              
                            </tr>
                            <tr>
								<td class="label-col"><label>Mô tả sản phẩm : </label></td>
								<td><textarea name="MoTa" style="height:70px"  placeholder="<?php  if(isset($_REQUEST['ThemSanPham']))echo $_REQUEST['MoTa'];  ?>" id="tbMota" ></textarea></td>
                            </tr>								
								<td>
									<div><input type="submit" value="Thêm mới" id="btthemsanpham" name="ThemSanPham" /></div><div><a href="index.php?com=SanPham&fun=add"><input type="reset" value="Làm lại" /></a></div>								  
								</td>
                            </tr>
                        </table>
					</form>
					</div>
				</div>
			</div>
			<?php
				require("../UserControl/footer.php");
			?>
		</div>
	</body>
</html>





